package info.wangyuan.agent.config;

import com.alibaba.ttl.TransmittableThreadLocal;
import info.wangyuan.agent.entity.po.User;
import lombok.Data;

/**
 * 用户上下文
 *
 * @author Albert
 * @since 2025-09-12 22:27:00
 */
public class UserContext {

    private static final TransmittableThreadLocal<Holder> holder = new TransmittableThreadLocal<>();

    /**
     * 设置当前用户
     */
    public static void setCurrentUser(User user) {
        Holder holder = UserContext.holder.get();
        holder.setUser(user);
    }

    /**
     * 获取当前用户
     */
    public static User getCurrentUser() {
        return holder.get().getUser();
    }

    public static void clear() {
        holder.remove();
    }

    @Data
    public static class Holder {
        private User user;
    }
}
